﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Loging
{
    class DBHelper
    {
        //创建数据库连接字符串conString
        private static readonly string conString = "server = .;database = Careers ; user = sa ; pwd = 569668 ";

        //创建数据库连接对象sqlConnection
        private static readonly SqlConnection sqlConnection = new SqlConnection(conString);

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable GetDataTable(string sql)
        {
            sqlConnection.Open();

            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql,sqlConnection);

            DataTable dt = new DataTable();

            sqlDataAdapter.Fill(dt);

            sqlConnection.Close();

            return dt;

        }

        /// <summary>
        /// 添加、删除、更新数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int InsertOrDeleteOrUpdate(string sql)
        {
            sqlConnection.Open();

            SqlCommand sqlCommand = new SqlCommand(sql,sqlConnection);

            var linecount = sqlCommand.ExecuteNonQuery();

            sqlConnection.Close();

            return linecount;
        }



    }
}
